package uz.edo.app.admin.userPages;

import org.apache.wicket.markup.html.basic.MultiLineLabel;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.markup.html.navigation.paging.PagingNavigator;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.markup.repeater.data.ListDataProvider;
import org.apache.wicket.model.CompoundPropertyModel;
import uz.edo.app.admin.AdminPage;
import uz.edo.db.bean.User;
import uz.edo.db.manager.UserManager;
import java.util.List;
import java.util.LinkedList;

/**
 * Created by IntelliJ IDEA.
 * User: said
 * Date: 1/24/12
 * Time: 12:25 PM
 * To change this template use File | Settings | File Templates.
 */
public class UserListView extends AdminPage{

    UserManager manager = UserManager.getInstance();

    public UserListView() throws Exception {
        final DataView dataView;
        List<User> users = new LinkedList<User>();
        for(User user : (List<User>)manager.getAll()){
            if(!isPrivileged("su") && user.isPrivileged("su"))
                continue;
            users.add(user);
        }
        add(dataView = new DataView("users",new ListDataProvider(users),ROWS_PER_PAGE) {
            @Override
            protected void populateItem(Item item) {
                final User user = (User)item.getModelObject();
                final CompoundPropertyModel userModel = new CompoundPropertyModel(user);
                item.setModel(userModel);
                item.add(new MultiLineLabel("userName"));
                item.add(new MultiLineLabel("password"));
                item.add(new MultiLineLabel("startDateStr"));
                item.add(new MultiLineLabel("endDateStr"));
                item.add(new MultiLineLabel("active"));
                item.add(new MultiLineLabel("rolesStr"));
                item.add(new MultiLineLabel("department.nameUz"));
                item.add(new Link("editLink"){
                    @Override
                    public void onClick() {
                        setResponsePage(new UserEdit(user));
                    }
                });
                item.add(new Link("deleteLink"){
                    @Override
                    public void onClick() {
                        try {
                            manager.delete(user);
                            setResponsePage(UserListView.class);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
            }
        });

        add(new PagingNavigator("navigator", dataView){
            public boolean isVisible() {
                return dataView.getPageCount() > 1;
            }
        });

        add(new Link("addLink") {
            @Override
            public void onClick() {
                goToNew();
            }
        });

        add(new Link("addLink2") {
            @Override
            public void onClick() {
                goToNew();
            }
        });
    }

    private void goToNew(){
        setResponsePage(UserAdd.class);
    }
}
